﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MHit : NState<BattleEntityHero>
{
    public void Enter(BattleEntityHero entity, params object[] args)
    {
        int hitType = (int)args[0];
        int dmgValue = 0;
        bool m_bPlayAni = (bool)args[2];
        float m_fDelaySendMessage = 0;
        if (!m_bPlayAni)
            m_fDelaySendMessage = (float)args[3];

        bool m_bChangeHp = true;
        if(hitType == HeroFightDef.c_iHitType_Dmg)
        {
            Dmg dmg = args[1] as Dmg;
            dmgValue = dmg.dmg;
            m_bChangeHp = !dmg.isGod;
        }
        else if(hitType == HeroFightDef.c_iHitType_BufDmg)
        {
            BufDmg bufDmg = args[1] as BufDmg;
            dmgValue = bufDmg.dmg;
            m_bChangeHp = true;
        }
        if (m_bPlayAni)
            entity.PlayAction(HeroFightDef.c_sAction_Hit, false);
        else
            EntityMessageDispatcher.Instance.SendMessage(HeroFightDef.c_sAction_Hit, entity.Id, entity.Id, m_fDelaySendMessage);

        if (m_bChangeHp)
            entity.HpChange(dmgValue);
    }

    public void Execute(BattleEntityHero entity, params object[] args)
    {
        
    }

    public void Exit(BattleEntityHero entity, params object[] args)
    {
        
    }

    public bool OnMessage(BattleEntityHero entity, MessageBody message)
    {
        if(message.msgId == HeroFightDef.c_sAction_Hit)
        {
            if (entity.IsDead())
            {
                entity.ChangeState(BattleHeroState.c_sMState_Dead);
            }
            else
            {
                entity.ChangeState(BattleHeroState.c_sMState_Idle);
                EntityMessageDispatcher.Instance.SendEntity2Node(message.msgId, entity.Id);
            }
        }
        return false;
    }
}
